<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Usersqrcodelists extends Model
{
    protected $table = 'users_qrcode_lists';
    protected $primaryKey = 'id';

    public static function batchInsertUserQrcodes($qrcodeLists)
    {
        return self::insert($qrcodeLists);
    }

    public static function deleteUserQrcodes($qrcodes_id)
    {
        return self::where("qrcodes_id",$qrcodes_id)->update(["status"=>3]);
    }

    public static function getUsersQrcodeLists($users_id,$links_id,$status,$created_start,$created_end)
    {
        $query = self::select("users_qrcode_lists.*")->leftJoin('links_lists', 'links_lists.links_id', '=', 'users_qrcode_lists.links_id');;
        if (!empty($users_id)) $query->where("users_qrcode_lists.users_id",$users_id);
        if (!empty($links_id)) $query->where("users_qrcode_lists.links_id",$links_id);
        if (!empty($status)) $query->where("users_qrcode_lists.status",$status);
        if (!empty($status)) $query->where("links_lists.end_time",">=",time());
        if (!empty($created_start)) $query->whereIn("users_qrcode_lists.created_at",[$created_start,$created_end]);
        return $query->orderBy("users_qrcode_lists.id","DESC")->paginate(50);
    }
}
